var paginationTotal = 1;//记录总数
$(function () {
//    initData(0); //初始化分页数据
    $("#edit").dialog({
        autoOpen:false,
        width:400,
        // height:280,
        modal:true,
        show:"slide",
        zIndex:100
    });
    $(".messageDiv").dialog({
        autoOpen:false,
        width:400,
        //height:200,
        modal:true,
        show:"slide",
        zIndex:200,
        title:"<{$lang.tip}><{$lang.msg}>",
        buttons:{
            "<{$lang.close}>":function () {
                $(this).dialog('close');
            }
           
        }
    });

    $("#confrimDiv").dialog({
        autoOpen:false,
        width:400,
        //height:200,
        modal:true,
        show:"slide",
        zIndex:200,
        title:"<{$lang.tip}><{$lang.msg}>"
    });
});

function loadData(page, pageSize) {
    var cn_name = $('[name=cn_name]').val();
    var en_name = $('[name=en_name]').val();
    $.ajax({
        type:"POST",
        async:false,
        dataType:"json",
        url:"/common/country/index",
        data:{
            'page':page,
            'pageSize':pageSize,
            'cn_name':cn_name,
            'en_name':en_name
        },
        success:function (json) {
            var html = "";
            paginationTotal = json.count;
            if (json.count == 0) {
                html = "<td colspan='8' class=''>&nbsp;<{$lang.no_data}></td>";
            } else {
				var i = (page == 1 ? 1 : pageSize * (page - 1) + 1);
                $.each(json.list, function (key, item) {
                    html += "<tr><td>"+ (i++) +"</td>";
                    html += "<td>" + item.cn_name + "</td>";
                    html += "<td>" + item.en_name + "</td>";
                    html += "<td>" + item.country_code + "</td>";
                    html += "<td>" + item.sort + "</td>";
                    html += "<td>" + item.short_name + "</td>";
                    html += "<td class=\"center\"><a href=\"javascript:editPage(" + item.country_id + ")\"><img src=\"/images/icon/edit.gif\" title=\"<{$lang.edit}>\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:void(0)\" onclick=\"alertInfo(" + item.country_id + ",'" + item.cn_name + "')\"><img src=\"/images/icon/delete.gif\" title=\"<{$lang.delete}>\"></a></td>";
                    html += "</tr>";
                })
            }
            $("#list").html(html);

        }
    });
}

function search() {
    initData(0);
}

function showEditPage(obj, titles, asc) {
    if (asc == 'add') {
        obj.dialog({
            title:titles,
            buttons:{
                '<{$lang.add}>':function () {
                    editSubmit()
                },
                '<{$lang.close}>':function () {
                    $(this).dialog('close');
                }
            }
        });
    } else {
        obj.dialog({
            title:titles,
            buttons:{
                '<{$lang.edit}>':function () {
                    editSubmit()
                },
                '<{$lang.close}>':function () {
                    $(this).dialog('close');
                }
            }
        });
    }
    obj.dialog("open");
}
function editPage(country_id) {
    if (country_id == 0) {
        $('#edit [name=country_id]').val(0);
        $('#editForm').resetForm();
        showEditPage($('#edit'), '<{$lang.add}><{$lang.country}>', '<{$lang.add}>');
        return;
    }
    $.ajax({
        type:"POST",
        async:false,
        dataType:"json",
        url:"/common/country/info", //请求的处理url
        data:{'country_id':country_id },
        success:function (json) {
            if (!json.ask) {
                var html = '<p class="alertMsg"><img src="/images/not.gif"><{$lang.country}><{$lang.msg}><{$lang.get}><{$lang.fail}></p>';
                $('#message').html(html);
                $('.messageDiv').dialog("open");
                return false;
            }
            var row = json.list;
            $('#edit [name=country_id]').val(row.country_id);
            $('#edit [name=cn_name]').val(row.cn_name);
            $('#edit [name=en_name]').val(row.en_name);
            $('#edit [name=country_alias]').val(row.country_alias);
            $('#edit [name=country_code]').val(row.country_code);
            $('#edit [name=sort]').val(row.sort);
            $('#edit [name=short_name]').val(row.short_name);
            $('#edit [name=st_discount]').val(row.st_discount);
            showEditPage($('#edit'), '<{$lang.edit}><{$lang.country}>', '<{$lang.edit}>');
        }
    });
}


function editSubmit() {
    var formData = $('#editForm').serialize();
    $.ajax({
        type:"POST",
        async:false,
        dataType:"json",
        url:"/common/country/edit", //请求的处理url
        data:formData,
        success:function (json) {
            var html = "";
            if (json.ask) {
                var html = '<p class="alertMsg"><img src="/images/yes.gif">' + json.list.message + '</p>';
                $('#message').html(html);
            } else {
                var error = json.list.error;
                $.each(error, function (key, item) {
                    html += '<p class="alertMsg"><img src="/images/no.gif">' + item + '</p>';
                });
                $('#message').html(html);
            }
            $('.messageDiv').dialog("open");
        }
    });
}

function alertInfo(country_id, name) {
    $("#confrimDiv").dialog({
        buttons:{
            "确认":function () {
                del(country_id, name)
            },
            "返回":function () {
                $(this).dialog('close')
            }
        }
    });
    $('#confrimMsg').html("<{$lang.delete}><{$lang.country}>" + name + '?');
    $('#confrimDiv').dialog("open");
}
function del(country_id, name) {
    $('#confrimDiv').dialog("close");
    $.ajax({
        type:"POST",
        async:false,
        dataType:"json",
        url:"/common/country/delete", //请求的处理url
        data:{'country_id':country_id},
        success:function (json) {
            var html = "";
            if (json.ask) {
                var html = '<p class="alertMsg"><img src="/images/yes.gif">' + json.list.message + '</p>';
                $('#message').html(html);
            } else {
                var error = json.list.error;
                $.each(error, function (key, item) {
                    html += '<p class="alertMsg"><img src="/images/no.gif">' + item + '</p>';
                });
                $('#message').html(html);
            }
            $('.messageDiv').dialog("open");
            var curPage = $(".pagination span> .current").html();
            initData(curPage);
        }
    });
}